package com.dms.modules.community.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dms.modules.community.entity.CommunityCategory;
import com.dms.modules.community.mapper.CommunityCategoryMapper;
import com.dms.modules.community.service.CommunityCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CommunityCategoryServiceImpl extends ServiceImpl<CommunityCategoryMapper, CommunityCategory> implements CommunityCategoryService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createCategory(String name, String description, String icon, Integer sort) {
        CommunityCategory category = new CommunityCategory();
        category.setName(name);
        category.setDescription(description);
        category.setIcon(icon);
        category.setSort(sort);
        category.setStatus(1);
        save(category);
        return category.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCategory(Long id, String name, String description, String icon, Integer sort, Integer status) {
        CommunityCategory category = getById(id);
        if (category == null) {
            return false;
        }
        category.setName(name);
        category.setDescription(description);
        category.setIcon(icon);
        category.setSort(sort);
        category.setStatus(status);
        return updateById(category);
    }

    @Override
    public IPage<CommunityCategory> getCategoryList(Page<CommunityCategory> page) {
        return lambdaQuery()
                .orderByAsc(CommunityCategory::getSort)
                .page(page);
    }
} 